package com.qjz.diancan.controller;

import com.github.pagehelper.PageInfo;
import com.qjz.diancan.model.entity.Cart;
import com.qjz.diancan.model.entity.Category;
import com.qjz.diancan.model.entity.Dish;
import com.qjz.diancan.model.entity.Order;
import com.qjz.diancan.model.vo.common.Result;
import com.qjz.diancan.model.vo.common.ResultCodeEnum;
import com.qjz.diancan.service.CartService;
import com.qjz.diancan.service.CategoryService;
import com.qjz.diancan.service.DishService;
import com.qjz.diancan.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Tag(name = "用户接口")
@RestController
@RequestMapping(value = "/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CartService cartService;

    @Autowired
    private OrderService orderService;

    @Operation(summary = "菜品分类接口")
    @GetMapping("/cate")
    public Result getDishCate(@RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize)
    {
        PageInfo<Category> categoryPageInfo = categoryService.findByPage(pageNum, pageSize);
        return Result.build(categoryPageInfo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除类目")
    @DeleteMapping("/cate/{id}")
    public Result deleteCate(@PathVariable(value = "id") Integer id)
    {
        categoryService.deleteCate(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "添加类目")
    @PostMapping("/cate")
    public Result addCate(@RequestBody Category category)
    {
        categoryService.addCategory(category);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改类目")
    @PutMapping("/cate")
    public Result updateCate(@RequestBody Category category)
    {
        categoryService.updateCate(category);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取菜品数据")
    @GetMapping("/data")
    public Result getDishData(@RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "categoryId", required = false) Integer categoryId, @RequestParam(value = "status", required = false) Integer status)
    {
        PageInfo<Dish> dishPageInfo = dishService.findByPage(pageNum, pageSize, name, categoryId, status);
        return Result.build(dishPageInfo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "添加菜品数据")
    @PostMapping("/data")
    public Result addDishData(@RequestBody Dish dish)
    {
        dishService.addDishData(dish);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改菜品状态")
    @PutMapping("/data/{id}/{state}")
    public Result updateDishDataState(@PathVariable(value = "id") Integer id, @PathVariable(value = "state") Integer state)
    {
        dishService.updateDishDataState(id, state);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改菜品数据")
    @PutMapping("/data")
    public Result updateDishData(@RequestBody Dish dish)
    {
        dishService.updateDishData(dish);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取菜品详情")
    @GetMapping("/data/{id}")
    public Result getDishData(@PathVariable(value = "id") Integer id)
    {
        Dish dish = dishService.getDishData(id);
        return Result.build(dish, ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "添加购物车")
    @PostMapping("/shopcar")
    public Result addShopCar(@RequestParam(value = "id") Integer userId, @RequestBody Dish dish)
    {
        cartService.addShopCar(userId, dish);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取购物车数据")
    @GetMapping("/shopcar")
    public Result getShopCarData(@RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize, @RequestParam(value = "id") Integer id)
    {
        PageInfo<Cart> cartPageInfo = cartService.findByPage(pageNum, pageSize, id);
        return Result.build(cartPageInfo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改购物车数据")
    @PutMapping("/shopcar")
    public Result updateShopCarData(@RequestBody Cart cart)
    {
        cartService.updateShopCarData(cart);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除购物车数据")
    @DeleteMapping("/shopcar/{id}")
    public Result deleteShopCarData(@PathVariable(value = "id") Integer id)
    {
        cartService.deleteShopCarData(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "批量清除购物车数据")
    @DeleteMapping("/shopcar")
    public Result deleteShopCarDataAll(@RequestParam(value = "ids") String ids)
    {
        String[] split = ids.split(",");
        for (String s : split) {
            cartService.deleteShopCarData(Integer.parseInt(s));
        }
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "创建订单")
    @PostMapping("/createOrder")
    public Result pay(@RequestParam Map<String, String> params)
    {
        Integer userId = Integer.valueOf(params.get("userId"));
        String ids = params.get("ids");
        String[] idStrings = ids.split(",");
        List<Integer> idIntegers = Arrays.stream(idStrings).map(Integer::parseInt).collect(Collectors.toList());
        orderService.pay(userId, idIntegers);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取订单数据")
    @GetMapping("/order")
    public Result getOrderData(@RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize, @RequestParam(value = "userId") Integer userId)
    {
        PageInfo<Order> orderPageInfo = orderService.findByPage(pageNum, pageSize, userId);
        return Result.build(orderPageInfo, ResultCodeEnum.SUCCESS);
    }

}
